package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.enums.OrderTypeEnum;
import com.xmy.cultivate.enums.StatusOrderApplyEnum;
import com.xmy.cultivate.exception.CommonException;
import com.xmy.cultivate.mapper.OrderDisPriceMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 优惠金额 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-06-27
 */
@Service
public class OrderDisPriceServiceImpl extends ServiceImpl<OrderDisPriceMapper, OrderDisPrice> implements IOrderDisPriceService {

    @Autowired
    @Lazy
    OrderDisPriceMapper orderDisPriceMapper;

    @Autowired
    @Lazy
    IOrderApplyService iOrderApplyService;

    @Autowired
    @Lazy
    IOrderDetailService iOrderDetailService;

    @Autowired
    @Lazy
    IOrderDisDetailService iOrderDisDetailService;

    @Autowired
    IOrderDisClanService iOrderDisClanService;

    @Override
    public Long addDisPrice(Long orderId, Long disOrderId, BigDecimal disPrice, LocalDate discountsDate){
        OrderDisPrice orderDisPrice = new OrderDisPrice();
        orderDisPrice.setDisPrice(disPrice);
        orderDisPrice.setOrderId(orderId);
        if (disOrderId == null){
            disOrderId = orderId;
        }
        orderDisPrice.setDisOrderId(disOrderId);
        if (discountsDate != null){
            orderDisPrice.setAddDate(discountsDate);
        }else {
            orderDisPrice.setAddDate(LocalDate.now());
        }

        this.save(orderDisPrice);
        return orderDisPrice.getId();
    }

    /**
     *
     * @param orderDisPrice
     * @param delType 1删除，2作废
     * @return
     */
    @Override
    @Transactional
    public boolean delDisPrice(OrderDisPrice orderDisPrice,Integer delType){

        OrderApply orderApplyInfo = iOrderApplyService.getById(orderDisPrice.getOrderId());
        if (orderApplyInfo== null){
            throw new CommonException(ResultCode.ORDERDETAIL_NOT);
        }

        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq("order_id",orderApplyInfo.getId());
        orderDetailQueryWrapper.orderByAsc("id");
        List<OrderDetail> orderDetailList =  iOrderDetailService.list(orderDetailQueryWrapper);
        //BigDecimal allDisPrice = disPrice;
        Integer i= 0;
        BigDecimal disPrice = orderDisPrice.getDisPrice();
        for (OrderDetail orderDetail:orderDetailList){
            if (i>0){
                continue;
            }
            orderDetail.setDiscountsPrice(orderDetail.getDiscountsPrice().subtract(disPrice));
            BigDecimal residuePrice = orderDetail.getResiduePrice().add(disPrice);
            orderDetail.setResiduePrice(residuePrice);
            BigDecimal solidPrice = orderDetail.getSolidPrice().add(disPrice);
            orderDetail.setSolidPrice(solidPrice);
            iOrderDetailService.updateById(orderDetail);
        }



        QueryWrapper<OrderDisDetail> orderDisDetailQueryWrapper = new QueryWrapper<>();
        orderDisDetailQueryWrapper.eq("order_id",orderDisPrice.getOrderId());
        orderDisDetailQueryWrapper.eq("dis_price_id",orderDisPrice.getId());
        iOrderDisDetailService.remove(orderDisDetailQueryWrapper);

        if (!orderDisPrice.getOrderId().equals(orderDisPrice.getDisOrderId())){
            OrderApply orderApplyDis = iOrderApplyService.getById(orderDisPrice.getDisOrderId());
            if (orderApplyDis!=null && orderApplyDis.getOrderType().equals(OrderTypeEnum.ORDER_TYPE_DIS)){
                if (delType.equals(1)){
                    iOrderApplyService.removeById(orderApplyDis);
                }else {
                    orderApplyDis.setStatus(StatusOrderApplyEnum.TOVOID);
                    orderApplyDis.setAbolishType(2);
                    iOrderApplyService.updateById(orderApplyDis);
                }
            }
        }else {
            BigDecimal allDisPriceApply = orderApplyInfo.getDisPrice().subtract(disPrice);
            orderApplyInfo.setDisPrice(allDisPriceApply);
            orderApplyInfo.setSolidPrice(orderApplyInfo.getSolidPrice().add(disPrice));
            iOrderApplyService.updateById(orderApplyInfo);
        }

        this.removeById(orderDisPrice);

        // 优惠表删除直系亲属优惠后、作废直系亲属优惠后 ——> 删除亲属
        QueryWrapper<OrderDisClan> orderDisClanQueryWrapper = new QueryWrapper<>();
        orderDisClanQueryWrapper.eq("order_id",orderDisPrice.getDisOrderId());
        iOrderDisClanService.remove(orderDisClanQueryWrapper);

        return true;
    }
}
